package com.tyl.xtk.config;

import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

@Configuration
public class JacksonConfig {
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer customizer() {
        return builder -> {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            // 把 LocalDateTime 的序列化/反序列化都改掉
            builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(df));
            builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(df));
            builder.timeZone(TimeZone.getDefault());
        };
    }
}